package zhoufu.sz.sanxiao_check.inject.bindadapters;

import android.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.target.Target;
import com.github.aakira.expandablelayout.ExpandableRelativeLayout;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.models.CheckItemModel;
import zhoufu.sz.sanxiao_check.models.DangerModel;
import zhoufu.sz.sanxiao_check.models.StreetInfoModel;
import zhoufu.sz.sanxiao_check.utils.UIUtils;
import zhoufu.sz.sanxiao_check.weiget.ImageWithDelView;

/**
 * 项目： SanXiao_Check
 * 时间： 2016/6/28 16:20
 * 人员： hyc
 * databinding的方法实现
 */

public class BuildBasicAdapter
{
    @BindingAdapter(value = { "Business" })
    public void setBusiness(TextView view, StreetInfoModel model)
    {
        if ( model == null )
        {
            view.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            return;
        }
        if ( "无".equals(model.getHas_Business()) || zhoufu.sz.sanxiao_check.utils.TextUtils
                .isEmpty(model.getSID()) )
        {
            view.setCompoundDrawablesWithIntrinsicBounds(R.drawable.btn_n_2x, 0, 0, 0);
        } else
        {
            view.setCompoundDrawablesWithIntrinsicBounds(R.drawable.btn_h_2x, 0, 0, 0);
        }
    }

    @BindingAdapter(value = { "StreetType" })
    public void setStreetType(TextView view, StreetInfoModel model)
    {
        if ( model == null )
        {
            view.setText(UIUtils.getString(R.string.street_basic_place, ""));
            return;
        }
        switch (model.getType())
        {
            case "1":
                view.setText(UIUtils.getString(R.string.street_basic_place, "小档口"));
                break;
            case "2":
                view.setText(UIUtils.getString(R.string.street_basic_place, "小作坊"));
                break;
            case "3":
                view.setText(UIUtils.getString(R.string.street_basic_place, "小娱乐场所"));
                break;
            default:
                view.setText(UIUtils.getString(R.string.street_basic_place, ""));
                break;
        }
    }

    @BindingAdapter("imageUrl")
    public void setImageUrl(ImageView view, String url)
    {
        if ( TextUtils.isEmpty(url) )
        {
            view.setImageResource(R.drawable.store_front_2_defult);
        } else
        {
            Glide.with(view.getContext()).load(url)
                    .override(UIUtils.getScreenWidth(UIUtils.getContext()), UIUtils.dip2px(120))
                    .crossFade().error(R.drawable.image_error).placeholder(R.drawable.image_loading)
                    .into(view);
        }
    }

    @BindingAdapter(value = { "imageUrl", "default", "error" }, requireAll = false)
    public void loadImage(ImageWithDelView imageView, String imageUrl, Drawable defaultImage,
            Drawable errorImage)
    {
        if ( TextUtils.isEmpty(imageUrl) )
        {
            return;
        }
        imageView.setImageSource(imageUrl);
    }

    @BindingAdapter(value = { "imageUrl", "default", "error" }, requireAll = false)
    public void loadImage(ImageView imageView, String imageUrl, Drawable defaultImage,
            Drawable errorImage)
    {
        imageView.setImageDrawable(null);
        if ( TextUtils.isEmpty(imageUrl) )
        {
            return;
        }
        Target<GlideDrawable> into = Glide.with(imageView.getContext()).load(imageUrl).crossFade()
                .into(imageView);
        into.onLoadStarted(defaultImage);
        into.onLoadFailed(null, errorImage);
    }

    @BindingAdapter(value = { "gradeImageUrl" })
    public void loadGradeImage(ImageView imageView, String imageUrl)
    {

        if ( TextUtils.isEmpty(imageUrl) )
        {
            imageView.setImageResource(R.drawable.add_photo);
            return;
        }
        Target<GlideDrawable> into = Glide.with(imageView.getContext()).load(imageUrl)
                .error(R.drawable.image_error).placeholder(R.drawable.image_loading)
                .override(UIUtils.dip2px(90), UIUtils.dip2px(90)).crossFade()
                .into(imageView);
    }

    @BindingAdapter(value = { "android:checkedButton" })
    public void rgCheckedChild(RadioGroup radioGroup, String IsLive)
    {
        int id = 0;
        if ( TextUtils.isEmpty(IsLive) )
        {
            id = R.id.mRbPeopleNo;
        } else
        {
            switch (IsLive)
            {
                case "0":
                    id = R.id.mRbPeopleNo;
                    break;
                case "1":
                    id = R.id.mRbPeopleYes;
                    break;
                default:
                    id = R.id.mRbPeopleNo;
                    break;
            }
        }
        radioGroup.check(id);
    }

    @BindingAdapter(value = { "android:checkType" })
    public void rgCheckedType(RadioGroup radioGroup, String grade)
    {
        int id = 0;
        if ( TextUtils.isEmpty(grade) )
        {
            id = R.id.mRbType1;
        } else
        {
            switch (grade)
            {
                case "1":
                    id = R.id.mRbType1;
                    break;
                case "2":
                    id = R.id.mRbType2;
                    break;
                case "3":
                    id = R.id.mRbType3;
                    break;
            }
        }
        radioGroup.check(id);
    }

    @BindingAdapter(value = { "android:checkID" })
    public void rgCheckedID(RadioGroup radioGroup, String ID)
    {
        int id = 0;
        if ( TextUtils.isEmpty(ID) )
        {
            id = R.id.mRbID;
        } else
        {
            switch (ID)
            {
                case "0":
                    id = R.id.mRbUNID;
                    break;
                case "1":
                    id = R.id.mRbID;
                    break;
            }
        }
        radioGroup.check(id);
    }

    @BindingAdapter(value = { "android:checkGrade" })
    public void rgcheckGrade(RadioGroup radioGroup, String grade)
    {
        int id = 0;
        if ( TextUtils.isEmpty(grade) )
        {
            id = R.id.mRbGrade1;
        } else
        {
            switch (grade)
            {
                case "A":
                    id = R.id.mRbGrade1;
                    break;
                case "B":
                    id = R.id.mRbGrade2;
                    break;
                case "C":
                    id = R.id.mRbGrade3;
                    break;
            }
        }
        radioGroup.check(id);
    }

    @BindingAdapter(value = { "android:checkFire" })
    public void radioFire(RadioGroup radioGroup, DangerModel model)
    {
        int id = 0;

        switch (model.getItem_ContentCheck())
        {
            case 0:
                id = R.id.cb_woodiness;
                break;
            case 1:
                id = R.id.cb_iron;
                break;
            case 2:
                id = R.id.cb_cement;
                break;
            case 3:
                id = R.id.cb_other;
                break;
        }
        radioGroup.check(id);
    }

    @BindingAdapter(value = { "android:expanded" })
    public void ael_expanded(ExpandableRelativeLayout layout, String SID)
    {
        if ( zhoufu.sz.sanxiao_check.utils.TextUtils.isEmpty(SID) )
        {
            layout.setExpanded(true);
        } else
        {
            layout.setExpanded(false);
        }
    }

    @BindingAdapter(value = { "android:text", "type" })
    public void printText(TextView textView, CheckItemModel model, int type)
    {
        if ( model == null )
        {
            return;
        }
        if ( model.getDangers() == null || model.getDangers().size() == 0 )
        {
            textView.setText("无");
        } else
        {
            StringBuffer sb = new StringBuffer();
            for (DangerModel dangerModel : model.getDangers())
            {
                if ( type == 1 )
                {//已整改
                    if ( dangerModel.getIs_Ok() == 1 && dangerModel.getDisposeStatus() == 1 )
                    {
                        sb.append("<font color=#A0A0A0><h6>");
                        sb.append(dangerModel.getContent());
                        sb.append("</h6></font>");
                        sb.append(dangerModel.getMeasure());
                    }
                } else
                {
                    if ( dangerModel.getIs_Ok() == 1 && dangerModel.getDisposeStatus() == 0 )
                    {
                        sb.append("<font color=#A0A0A0><h6>");
                        sb.append(dangerModel.getContent());
                        sb.append("</h6></font>");
                        sb.append(dangerModel.getMeasure());
                    }
                }
            }
            if ( sb.length() > 0 )
            {
                textView.setText(Html.fromHtml(sb.toString()));
            } else
            {
                textView.setText("无");
            }
        }
    }
}
